package zcx;

/**
 * description:
 * author:zcx
 * lenovo
 * 时间：2022.02.07.20.07
 */
public class Wg1 {
    public static void main(String[] args) {
        TicketThread t = new TicketThread();
        new Thread(t,"t1").start();
        new Thread(t,"t2").start();
        new Thread(t,"t3").start();
        new Thread(t,"t4").start();
    }
}
    class  TicketThread implements Runnable {
        private int ticketNum = 10;
        @Override
        public void run() {
            while (true) {
                ticket();
                if(ticketNum==0){
                    System.out.println(Thread.currentThread().getName()+":"+"票已售完");
                break;}
            }
        }
        private synchronized void ticket() {
            if (ticketNum > 0) {
                System.out.println(Thread.currentThread().getName()+":"+ticketNum);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                ticketNum--;
            }
        }
    }

